/* 
 * AndFollowerStat
 * 
 * Copyright (C) 2009  Daniel Czerwonk <d.czerwonk@googlemail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.dan_nrw.android.followerstat.widget;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import de.dan_nrw.android.followerstat.AndroidFollowerStatActivity;
import de.dan_nrw.android.followerstat.R;
import de.dan_nrw.android.followerstat.TwitterUserCredentials;
import de.dan_nrw.android.followerstat.dao.folllowers.ITwitterFollowerDAO;
import de.dan_nrw.android.followerstat.dao.folllowers.TwitterFollowerDAO;
import de.dan_nrw.android.followerstat.dao.folllowers.TwitterRequestException;
import de.dan_nrw.android.followerstat.dao.folllowers.TwitterResponseParsingException;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;


/**
 * @author Daniel Czerwonk
 *
 */
public class UpdateFollowerStatWidgetService extends Service {

	public static String TWITTER_USERNAME = "TwitterUserName";
	
	private final ITwitterFollowerDAO followerDAO = TwitterFollowerDAO.createInstance(new TwitterUserCredentials()); 
	
	
	/* (non-Javadoc)
     * @see android.app.Service#onBind(android.content.Intent)
     */
    @Override
    public IBinder onBind(Intent arg0) {
    	// binding is not needed
        return null;
    }

	/* (non-Javadoc)
     * @see android.app.Service#onStart(android.content.Intent, int)
     */
    @Override
    public void onStart(Intent intent, int startId) {
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget);
        
        String userName = intent.getStringExtra(TWITTER_USERNAME);
        
        if (userName.equals("")) {
        	this.setErrorMeesage(this.getString(R.string.noUserNameDefinedErrorText), remoteViews);
        	
        	this.updateWidget(remoteViews);
        	
        	return;
        }
        
        remoteViews.setTextViewText(R.id.userNameTextView, String.format("@%s", userName));
        
        try {
            remoteViews.setTextViewText(R.id.followerCountTextView, 
            							String.format(this.getString(R.string.followersPattern), 
            										  this.followerDAO.getFollowerCount(userName)));
        }
        catch (TwitterResponseParsingException ex) {
        	this.setErrorMeesage(this.getString(R.string.parsingErrorText), remoteViews);
        }
        catch (TwitterRequestException ex) {
        	this.setErrorMeesage(this.getString(R.string.requestErrorText), remoteViews);
        }
        
        this.updateWidget(remoteViews);
    }
    
    private void setErrorMeesage(String message, RemoteViews remoteViews) {
    	remoteViews.setTextViewText(R.id.followerCountTextView, message);
    	remoteViews.setTextColor(R.id.followerCountTextView, R.color.ErrorColor);
    }
    
    private void updateWidget(RemoteViews remoteViews) {
    	DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(this);
    	DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(this); 
    	Date date = Calendar.getInstance().getTime(); 	
    	
    	String dateString = String.format("%s %s", dateFormat.format(date), timeFormat.format(date));
    	
    	remoteViews.setTextViewText(R.id.lastUpdateTextView, dateString);							

    	Intent intent = new Intent(Intent.ACTION_VIEW);
    	intent.setClass(this, AndroidFollowerStatActivity.class);
    	
    	remoteViews.setOnClickPendingIntent(R.id.widget, PendingIntent.getActivity(this, 0, intent, 0));
    	
        ComponentName widgetComponentName = new ComponentName(this, StatWidget.class);
        AppWidgetManager.getInstance(this).updateAppWidget(widgetComponentName, remoteViews);
    }
}